//
//  YWSelectBuildingTableViewController.m
//  maintain
//
//  Created by Kyo on 16/9/5.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWSelectBuildingTableViewController.h"
#import "UIColor+CustomColors.h"
#import "YWCommonTool.h"
#import "YWMacro.h"
#import "YWLoginTool.h"

#import "YWBuilding.h"
#import "YWBuildingManager.h"

@interface YWSelectBuildingTableViewController (){
    YWBuilding *selectedBuilding;
    NSIndexPath *lastIndexPath;
}
@property (nonatomic, strong) NSArray *allBuildings;
@end

@implementation YWSelectBuildingTableViewController
- (instancetype)init{
    return  [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *buildingShowName = [YWLoginTool buildingShowName];
    self.title = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"select", nil),buildingShowName.length?buildingShowName:NSLocalizedString(@"building", nil)];
    [self initTableView];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"finish", nil) style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemEvent:)];
}

- (void)initTableView{
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorColor = [UIColor customLineColor];
    self.tableView.rowHeight = 44.0f;
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    [self.tableView setEditing:YES];
}

- (NSArray *)allBuildings{
    if (!_allBuildings) {
        _allBuildings = [[NSArray alloc] initWithArray:[[YWBuildingManager shareManager] allBuilding]];
    }
    return _allBuildings;
}

- (void)setOwnBuilding:(YWBuilding *)ownBuilding{
    _ownBuilding = ownBuilding;
    if (ownBuilding) {
        [self.allBuildings enumerateObjectsUsingBlock:^(YWBuilding*  _Nonnull building, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([building.buildingId isEqualToString:ownBuilding.buildingId]) {
                selectedBuilding = building;
                *stop = YES;
            }
        }];
    }
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allBuildings.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"YWSelectBuildingTableVCReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.textLabel.font = kContentFont;
    }
    YWBuilding *building = self.allBuildings[indexPath.row];
    cell.textLabel.text = building.buildingName?:@"";
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    YWBuilding *building = self.allBuildings[indexPath.row];
    if ([building.buildingId isEqualToString:selectedBuilding.buildingId]) {
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }else{
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWBuilding *building = self.allBuildings[indexPath.row];
    if (![building.buildingId isEqualToString:selectedBuilding.buildingId]) {
        selectedBuilding = building;
    }
    if (lastIndexPath) {
        [tableView deselectRowAtIndexPath:lastIndexPath animated:NO];
    }else{
        [self.tableView reloadData];
    }
    lastIndexPath = indexPath;
    
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWBuilding *building = self.allBuildings[indexPath.row];
    if ([building.buildingId isEqualToString:selectedBuilding.buildingId]) {
        selectedBuilding = nil;
    }
    lastIndexPath = nil;
}

- (void)rightBarButtonItemEvent:(id)sender{
    if (selectedBuilding) {
        if ([self.delegate respondsToSelector:@selector(popSelectedBuilding:)]) {
            [self.delegate popSelectedBuilding:selectedBuilding];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else{
        NSString *buildingShowName = [YWLoginTool buildingShowName];
        [YWCommonTool showTextHUDWithContent:[NSString stringWithFormat:@"%@%@",NSLocalizedString(@"pleaseSelectOne", nil),buildingShowName.length?buildingShowName:NSLocalizedString(@"building", nil)]];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
